package org.javaforever.cookieshop.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.javaforever.cookieshop.utils.PriceUtils;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;

@Entity
@Table(name="cs_cust_orders")
public class CustOrderBean implements Comparable<CustOrderBean>,Serializable {
	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	protected Long id;

	@Column(name = "name")
	protected String name;

	@Column(name = "active")
	protected Boolean active;

	@Column(name = "total")
	protected Double total = 0d;

	@Column(name = "amount")
	protected Integer amount = 0;

	@Column(name = "status")
	protected Integer status;

	@Column(name = "paytype")
	protected Integer paytype;

	@Column(name = "phone")
	protected String phone;

	@Column(name = "address")
	protected String address;

	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
	@Column(name = "order_date_time")
	protected Date orderDateTime;

	@Column(name = "user")
	protected Long user;
	
	@Transient
	protected UserBean userBean;

	@Transient
    private Map<Long,OrderItemBean> itemMap = new HashMap<Long,OrderItemBean>();
	
	@Transient
    private List<OrderItemBean> itemList = new ArrayList<>();

	public Boolean getActive(){
		return this.active;
	}

	public String getAddress(){
		return this.address;
	}

	public Integer getAmount(){
		return this.amount;
	}

	public Long getId(){
		return this.id;
	}

	public String getName(){
		return this.name;
	}

	public Date getOrderDateTime(){
		return this.orderDateTime;
	}

	public Integer getPaytype(){
		return this.paytype;
	}

	public String getPhone(){
		return this.phone;
	}

	public Integer getStatus(){
		return this.status;
	}

	public Double getTotal(){
		return this.total;
	}

	public Long getUser(){
		return this.user;
	}

	public void setActive(Boolean active){
		this.active = active;
	}

	public void setAddress(String address){
		this.address = address;
	}

	public void setAmount(Integer amount){
		this.amount = amount;
	}

	public void setId(Long id){
		this.id = id;
	}

	public void setName(String name){
		this.name = name;
	}

	public void setOrderDateTime(Date orderDateTime){
		this.orderDateTime = orderDateTime;
	}

	public void setPaytype(Integer paytype){
		this.paytype = paytype;
	}

	public void setPhone(String phone){
		this.phone = phone;
	}

	public void setStatus(Integer status){
		this.status = status;
	}

	public void setTotal(Double total){
		this.total = total;
	}

	public void setUser(Long user){
		this.user = user;
	}

	public CustOrderBean clone(){
	    CustOrderBean custOrder0 = new CustOrderBean();
	    custOrder0.setId(this.getId());
	    custOrder0.setName(this.getName());
	    custOrder0.setActive(this.getActive());
	    custOrder0.setTotal(this.getTotal());
	    custOrder0.setAmount(this.getAmount());
	    custOrder0.setStatus(this.getStatus());
	    custOrder0.setPaytype(this.getPaytype());
	    custOrder0.setPhone(this.getPhone());
	    custOrder0.setAddress(this.getAddress());
	    custOrder0.setOrderDateTime(this.getOrderDateTime());
	    custOrder0.setUser(this.getUser());
	    return custOrder0;
	}

	public int compareTo(CustOrderBean custOrder){
        return this.getId().compareTo(custOrder.getId());
	}
	
	public CustOrderBean() {
		super();
		this.total = 0d;
		this.amount = 0;
	}

    public void addGoods(GoodsBean g) {
        if(itemMap.containsKey(g.getId())) {
            OrderItemBean item = itemMap.get(g.getId());
            item.setAmount(item.getAmount()+1);
        }else {
            OrderItemBean item = new OrderItemBean(g.getPrice(),1,g,this);
            itemMap.put(g.getId(), item);
        }
        amount++;
        total = PriceUtils.add(total, g.getPrice());
    }

	public Map<Long, OrderItemBean> getItemMap() {
		return itemMap;
	}

	public void setItemMap(Map<Long, OrderItemBean> itemMap) {
		this.itemMap = itemMap;
	}

	public List<OrderItemBean> getItemList() {
		return itemList;
	}

	public void setItemList(List<OrderItemBean> itemList) {
		this.itemList = itemList;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}
}
